<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      linux/入门到精通/10-Linux输入输出 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>linux/入门到精通/10-Linux输入输出</h2>



  <p class="post-date">2020-07-06</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="1-重定向概述"><a href="#1-重定向概述" class="headerlink" title="1.重定向概述"></a>1.重定向概述</h2><h3 id="1-什么是重定向"><a href="#1-什么是重定向" class="headerlink" title="1.什么是重定向"></a>1.什么是重定向</h3><p>将原本要输出到屏幕的数据信息，重新定向到某个指定的文件中。比如：每天凌晨定时备份数据，希望将备份数据的结果保存到某个文件中。这样第二天通过查看文件的内容就知道昨天备份的数据是成功还是失败。</p>
<h3 id="2-为何要使用重定向"><a href="#2-为何要使用重定向" class="headerlink" title="2.为何要使用重定向"></a>2.为何要使用重定向</h3><p>1.当屏幕输出的信息很重要，而且希望保存重要的信息时；<br>2.后台执行中的程序，不希望他干扰屏幕正常的输出结果时；<br>3.系统的例行命令, 例如定时任务的执行结果，希望可以存下来时；<br>4.一些执行命令，我们已经知道他可能出现错误信息, 想将他直接丢弃时;<br>5.错误日志与正确日志需要分别输出至不同的文件保存时;</p>
<h3 id="3-学习重定向的预备知识，标准输入与输出"><a href="#3-学习重定向的预备知识，标准输入与输出" class="headerlink" title="3.学习重定向的预备知识，标准输入与输出"></a>3.学习重定向的预备知识，标准输入与输出</h3><p>当运行一个程序时通常会自动打开三个标准文件，分别是标准输入、标准输出、错误输出</p>
<table>
<thead>
<tr>
<th align="left">名称</th>
<th align="left">文件描述符</th>
<th align="left">作用</th>
</tr>
</thead>
<tbody><tr>
<td align="left">标准输入（STDIN）</td>
<td align="left">0</td>
<td align="left">默认是键盘，也可以是文件或其他命令的输出。</td>
</tr>
<tr>
<td align="left">标准输出（STDOUT）</td>
<td align="left">1</td>
<td align="left">默认输出到屏幕。</td>
</tr>
<tr>
<td align="left">错误输出（STDERR）</td>
<td align="left">2</td>
<td align="left">默认输出到屏幕。</td>
</tr>
<tr>
<td align="left">文件名称（filename）</td>
<td align="left">3+</td>
<td align="left"></td>
</tr>
</tbody></table>
<p>进程将从标准输入中得到数据，将正常输出打印至屏幕终端，将错误的输出信息也打印至屏幕终端。<br>PS: 进程是使用文件描述符<code>(file descriptors)</code>来管理打开的文件</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu6zkd78jj30yc0aiq4f.jpg" alt="img"></p>
<p>以 cat 命令为例, cat 命令的功能是从命令行给出的文件中读取数据，并将这些数据直接送到标准输出。若使用如下命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#会把文件/etc/passwd的内容输出显示到屏幕上</span></span><br><span class="line">[root@lqz ~]<span class="comment"># cat /etc/passwd</span></span><br></pre></td></tr></table></figure>

<p>但如果 使用 cat 命令没有跟上输入的文件名，那么cat命令则会通过命令行标准输入中读取数据, 并将其送到标准输出。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># cat</span></span><br><span class="line">hello   <span class="comment">#标准输入</span></span><br><span class="line">hello   <span class="comment">#标准输出</span></span><br><span class="line">^C</span><br><span class="line"><span class="comment">#用户输入的每一行都立刻被cat命令输出到屏幕上。</span></span><br></pre></td></tr></table></figure>

<p>下面了解一下标准输入输出过程</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#持续追踪查看文件内容</span></span><br><span class="line">[root@lqz ~]<span class="comment"># tail -f /etc/passwd</span></span><br><span class="line">ctrl+z 将进程转到后台</span><br><span class="line"></span><br><span class="line"><span class="comment">#查看运行的进程</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ps</span></span><br><span class="line">PID TTY          TIME CMD</span><br><span class="line">5848 pts/1    00:00:00 bash</span><br><span class="line">6885 pts/1    00:00:00 tail</span><br><span class="line">6888 pts/1    00:00:00 ps</span><br><span class="line"></span><br><span class="line"><span class="comment">#查看tail命令的pid，6885进程下的文件描述符</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls -l /proc/6885/fd</span></span><br><span class="line">total 0</span><br><span class="line">lrwx------ 1 root root 64 Dec  3 06:57 0 -&gt; /dev/pts/1</span><br><span class="line">lrwx------ 1 root root 64 Dec  3 06:57 1 -&gt; /dev/pts/1</span><br><span class="line">lrwx------ 1 root root 64 Dec  3 06:56 2 -&gt; /dev/pts/1</span><br><span class="line">lr-x------ 1 root root 64 Dec  3 06:57 3 -&gt; /etc/passwd</span><br><span class="line">lr-x------ 1 root root 64 Dec  3 06:57 4 -&gt; inotify</span><br><span class="line"></span><br><span class="line"><span class="comment">#Linux查看标准输入输出设备</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls -l /dev/std</span></span><br><span class="line">lrwxrwxrwx 1 root root 15 Dec  2 22:30 /dev/stderr -&gt; /proc/self/fd/2</span><br><span class="line">lrwxrwxrwx 1 root root 15 Dec  2 22:30 /dev/stdin -&gt; /proc/self/fd/0</span><br><span class="line">lrwxrwxrwx 1 root root 15 Dec  2 22:30 /dev/stdout -&gt; /proc/self/fd/1</span><br></pre></td></tr></table></figure>

<h2 id="2-输出重定向"><a href="#2-输出重定向" class="headerlink" title="2.输出重定向"></a>2.输出重定向</h2><p>输出重定向，改变输出内容的位置。输出重定向有如下几种方式，如表格所示</p>
<table>
<thead>
<tr>
<th align="left">类型</th>
<th align="left">操作符</th>
<th align="left">用途</th>
</tr>
</thead>
<tbody><tr>
<td align="left">标准覆盖输出重定向</td>
<td align="left">&gt;</td>
<td align="left">将程序输出的正确结果输出到指定的文件中,会覆盖文件原有的内容</td>
</tr>
<tr>
<td align="left">标准追加输出重定向</td>
<td align="left">&gt;&gt;</td>
<td align="left">将程序输出的正确结果以追加的方式输出到指定文件，不会覆盖原有文件</td>
</tr>
<tr>
<td align="left">错误覆盖输出重定向</td>
<td align="left">2&gt;</td>
<td align="left">将程序的错误结果输出到执行的文件中，会覆盖文件原有的内容</td>
</tr>
<tr>
<td align="left">错误追加输出重定向</td>
<td align="left">2&gt;&gt;</td>
<td align="left">将程序输出的错误结果以追加的方式输出到指定文件，不会覆盖原有文件</td>
</tr>
<tr>
<td align="left">标准输入重定向</td>
<td align="left">&lt;&lt;</td>
<td align="left">将命令中接收输入的途径由默认的键盘更改为指定的文件或命令</td>
</tr>
</tbody></table>
<p>案例1: 标准输出重定向(每次都会覆盖文件)</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu6zr5e8fj30wi080t9p.jpg" alt="img"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#标准输出重定向, 先清空,后写入, 如果文件不存在则创建</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ifconfig eth0 &gt; abc</span></span><br></pre></td></tr></table></figure>

<p>案例2: 标准输出重定向(会往文件的尾部在添加内容)</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu6zuf2cij30xw07sab7.jpg" alt="img"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#标准追加输出重定向, 向配置文件末尾追加内容</span></span><br><span class="line">[lqz@lqz ~]$ <span class="built_in">echo</span> <span class="string">"This is network conf"</span> &gt;&gt; <span class="keyword">if</span></span><br></pre></td></tr></table></figure>

<p>案例3: 错误输出重定向</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu6zynvp1j30ve07gt9o.jpg" alt="img"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#正确输出以及错误输出重定向至一个文件</span></span><br><span class="line">[root@lqz ~]<span class="comment"># useradd lqz</span></span><br><span class="line">[root@lqz ~]<span class="comment"># su - lqz</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#将标准输出和标准错误输出重定向到不同文件</span></span><br><span class="line">[lqz@lqz ~]$ find /etc -name <span class="string">".conf"</span> 1&gt;a 2&gt;b</span><br></pre></td></tr></table></figure>

<p>案例4: 正确和错误都输入到相同位置<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu7036uqqj30ty07caay.jpg" alt="img"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#将标准输出和标准错误输出重定向到同一个文件, 混合输出</span></span><br><span class="line">[lqz@lqz ~]$ find /etc -name <span class="string">".conf"</span> &amp;&gt;ab</span><br><span class="line"></span><br><span class="line"><span class="comment">#合并两个文件内容至一个文件</span></span><br><span class="line">[lqz@lqz ~]$ cat a b &gt; c</span><br></pre></td></tr></table></figure>

<p>案例5: 正确和错误都输入到相同位置<br><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu7073zxqj30ti082t9p.jpg" alt="img"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#重定向到相同的位置</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls /root /error &gt;ab  2&gt;&amp;1</span></span><br></pre></td></tr></table></figure>

<p>案例6: 重定向到空设备/dev/null</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu70hz0o2j30xo0840tv.jpg" alt="img"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#将产生的任何数据放入黑洞设备，则视为丢弃。</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls /root /error &gt;ab 2&gt;/dev/null</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls /root /error &gt;ab &amp;&gt;/dev/null</span></span><br></pre></td></tr></table></figure>

<p>案例7: 脚本中使用重定向 (了解即可)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># vim ping.sh </span></span><br><span class="line">ping -c1 10.0.0.1</span><br><span class="line"><span class="keyword">if</span> [ $? -eq 0 ];<span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"10.0.0.1 is up."</span> </span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"10.0.0.1 is down."</span> </span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line">[root@lqz ~]<span class="comment"># chmod +x ping.sh </span></span><br><span class="line">[root@lqz ~]<span class="comment"># ./ping.sh</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#改进后版</span></span><br><span class="line">[root@lqz ~]<span class="comment"># vim ping.sh</span></span><br><span class="line">ping -c1 10.0.0.1 &amp;&gt;/dev/null</span><br><span class="line"><span class="keyword">if</span> [ $? -eq 0 ];<span class="keyword">then</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"10.0.0.1 is up."</span> </span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">"10.0.0.1 is down."</span> </span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<p>案例8: 脚本中使用重定向 (了解即可)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]# vim ping2.sh </span><br><span class="line">ping -c1 10.0.0.1 &amp;&gt;&#x2F;dev&#x2F;null </span><br><span class="line">if [ $? -eq 0 ];then</span><br><span class="line">    echo &quot;10.0.0.1 is up.&quot; &gt;&gt;up.txt </span><br><span class="line">else</span><br><span class="line">    echo &quot;10.0.0.1 is down.&quot; &gt;&gt;down.txt </span><br><span class="line">fi</span><br><span class="line">[root@lqz ~]# chmod +x ping2.sh </span><br><span class="line">[root@lqz ~]# .&#x2F;ping2.sh</span><br></pre></td></tr></table></figure>

<h2 id="3-输入重定向"><a href="#3-输入重定向" class="headerlink" title="3.输入重定向"></a>3.输入重定向</h2><p>输入重定向，即原本从键盘等上获得的输入信息，重定向由命令的输出作为输入。&lt; 等价 0&lt;</p>
<p>案例1: 从文件中读入输入的操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#没有改变输入的方向，默认键盘</span></span><br><span class="line">[root@lqz ~]<span class="comment"># mail alice </span></span><br><span class="line">Subject: hello</span><br><span class="line">1111 </span><br><span class="line">2222</span><br><span class="line">3333</span><br><span class="line">.   <span class="comment">#结束</span></span><br><span class="line">EOT</span><br><span class="line"></span><br><span class="line"><span class="comment">#检查是否收到邮件</span></span><br><span class="line">[root@lqz ~]<span class="comment"># su - alice</span></span><br><span class="line">[root@lqz ~]<span class="comment"># mail</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#输入重定向，来自于文件</span></span><br><span class="line">[root@lqz ~]<span class="comment"># mail -s "test01" alice &lt; /etc/hosts</span></span><br></pre></td></tr></table></figure>

<p>案例2: 无法形容案例，请看实际操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#没有改变输入的方向，默认键盘，此时等待输入</span></span><br><span class="line">[root@lqz ~]<span class="comment"># grep 'root' </span></span><br><span class="line">xxx</span><br><span class="line">xxx</span><br><span class="line"></span><br><span class="line">[root@lqz ~]<span class="comment"># grep 'root' &lt; /etc/passwd</span></span><br><span class="line">root:x:0:0:root:/root:/bin/bash</span><br></pre></td></tr></table></figure>

<p>案例3: 无法形容案例，请看实际操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># dd if=/dev/zero of=/file1.txt bs=1M count=20</span></span><br><span class="line">[root@lqz ~]<span class="comment"># dd &lt;/dev/zero &gt;/file2.txt bs=1M count=20</span></span><br></pre></td></tr></table></figure>

<p>案例4: mysql如何恢复备份，了解即可，不用关注。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># mysql -uroot -p123 &lt; bbs.sql</span></span><br></pre></td></tr></table></figure>

<p>案例5: 利用重定向建立多行数据的文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#手动执行 shell 命令</span></span><br><span class="line">[root@lqz ~]<span class="comment"># echo "111" &gt; file1.txt </span></span><br><span class="line">[root@lqz ~]<span class="comment"># cat file1.txt</span></span><br><span class="line">111</span><br><span class="line">[root@lqz ~]<span class="comment"># cat &gt;file2.txt</span></span><br><span class="line">111</span><br><span class="line">222</span><br><span class="line">333</span><br><span class="line">^D</span><br><span class="line"></span><br><span class="line">[root@lqz ~]<span class="comment"># cat &gt;&gt;file3.txt</span></span><br><span class="line">aaa</span><br><span class="line">bbb</span><br><span class="line">ccc</span><br><span class="line">^D</span><br></pre></td></tr></table></figure>

<p>案例6: 脚本中打印菜单的一种使用方法。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># vim vm.sh</span></span><br><span class="line">cat &lt;&lt;-EOF</span><br><span class="line">+------------------- --- ---- --- ---- --- --- ---- --- --+ ||</span><br><span class="line">| ====================== | </span><br><span class="line">| 虚拟机基本管理 v5.0 |</span><br><span class="line">| by lqz |</span><br><span class="line">| ====================== | </span><br><span class="line">| 1. 安装 KVM |</span><br><span class="line">| 2. 安装或重置 CentOS-6.9 | </span><br><span class="line">| 3. 安装或重置 CentOS-7.4 | </span><br><span class="line">| 5. 安装或重置 Windows-7  | </span><br><span class="line">| 6. 删除所有虚拟机 |</span><br><span class="line">| q. 退出管理程序 |</span><br><span class="line">+------------------- --- ---- --- ---- --- --- ---- --- --+ </span><br><span class="line">EOF</span><br></pre></td></tr></table></figure>

<p>案例7: 两条命令同时重定向</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># ls; date &amp;&gt;/dev/null</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls &amp;&gt;/dev/null; date &amp;&gt;/dev/null</span></span><br><span class="line">[root@lqz ~]<span class="comment"># (ls; date) &amp;&gt;/dev/null</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#后台执行</span></span><br><span class="line">[root@lqz ~]<span class="comment"># (while :; do date; sleep 2; done) &amp;</span></span><br><span class="line">[1] 6378</span><br><span class="line">[root@lqz ~]<span class="comment"># (while :; do date; sleep 2; done) &amp;&gt;date.txt &amp;</span></span><br><span class="line">[root@lqz ~]<span class="comment"># jobs</span></span><br><span class="line">[1]+ 运行中 ( <span class="keyword">while</span> :; <span class="keyword">do</span> date; sleep 2;</span><br><span class="line"><span class="keyword">done</span> ) &amp;&gt;/date.txt &amp;</span><br></pre></td></tr></table></figure>

<p>扩展点: subshell 了解即可</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># cd /boot; ls</span></span><br><span class="line"></span><br><span class="line">//subshell 中执行</span><br><span class="line">[root@lqz ~]<span class="comment"># (cd /boot; ls)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#如果不希望某些命令的执行对当前 shell 环境产生影响，请在subshell中执行</span></span><br></pre></td></tr></table></figure>

<h2 id="4-进程管道技术"><a href="#4-进程管道技术" class="headerlink" title="4.进程管道技术"></a>4.进程管道技术</h2><h3 id="1-什么是管道"><a href="#1-什么是管道" class="headerlink" title="1.什么是管道"></a>1.什么是管道</h3><p>管道操作符号 “|” ，主要用来连接左右两个命令, 将左侧的命令的标准输出, 交给右侧命令的标准输入<br>PS: 无法传递标准错误输出至后者命令</p>
<h3 id="2-管道流程示意图"><a href="#2-管道流程示意图" class="headerlink" title="2.管道流程示意图"></a>2.管道流程示意图</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu70of8auj30wk07kdgh.jpg" alt="img"><br>格式: <code>cmd1 | cmd2 [...|cmdn]</code></p>
<h3 id="3-管道使用案例"><a href="#3-管道使用案例" class="headerlink" title="3.管道使用案例"></a>3.管道使用案例</h3><p>案例1: 将/etc/passwd 中的用户按 UID 大小排序</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># sort -t":" -k3 -n /etc/passwd</span></span><br><span class="line">[root@lqz ~]<span class="comment"># sort -t":" -k3 -n /etc/passwd -r</span></span><br><span class="line">[root@lqz ~]<span class="comment"># sort -t":" -k3 -n /etc/passwd |head</span></span><br></pre></td></tr></table></figure>

<p>案例2: 统计当前/etc/passwd 中用户使用的 shell 类型</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#思路:取出第七列(shell) | 排序(把相同归类)| 去重</span></span><br><span class="line">[root@lqz ~]<span class="comment"># awk -F: '&#123;print $7&#125;' /etc/passwd</span></span><br><span class="line">[root@lqz ~]<span class="comment"># awk -F: '&#123;print $7&#125;' /etc/passwd |sort</span></span><br><span class="line">[root@lqz ~]<span class="comment"># awk -F: '&#123;print $7&#125;' /etc/passwd |sort |uniq</span></span><br><span class="line">[root@lqz ~]<span class="comment"># awk -F: '&#123;print $7&#125;' /etc/passwd |sort |uniq -c</span></span><br></pre></td></tr></table></figure>

<p>案例4: 统计网站的访问情况 top 20</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#思路: 打印所有访问的连接 | 过滤访问网站的连接 | 打印用户的 IP | 排序 | 去重</span></span><br><span class="line"></span><br><span class="line">[root@lqz ~]<span class="comment"># yum -y install httpd</span></span><br><span class="line">[root@lqz ~]<span class="comment"># systemctl start httpd</span></span><br><span class="line">[root@lqz ~]<span class="comment"># systemctl stop firewalld</span></span><br><span class="line"></span><br><span class="line">[root@lqz ~]<span class="comment"># ss -an |grep :80 |awk -F":" '&#123;print $8&#125;' |sort |uniq -c</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ss -an |grep :80 |awk -F":" '&#123;print $8&#125;' |sort |uniq -c |sort -k1 -rn |head -n 20</span></span><br></pre></td></tr></table></figure>

<p>案例5: 打印当前所有 IP</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># ip addr |grep 'inet ' |awk '&#123;print $2&#125;' |awk -F"/" '&#123;print $1&#125;'</span></span><br><span class="line">127.0.0.1</span><br><span class="line">192.168.69.112</span><br></pre></td></tr></table></figure>

<p>案例6: 打印根分区已用空间的百分比(仅打印数字)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># df |grep '/$' |awk '&#123;print $5&#125;' |awk -F"%" '&#123;print $1&#125;'</span></span><br></pre></td></tr></table></figure>

<p>PS: 管道命令符能让大家能进一步掌握命令之间的搭配使用方法，进一步提高命令输出值的处理效率。</p>
<h3 id="4-管道中的tee技术"><a href="#4-管道中的tee技术" class="headerlink" title="4.管道中的tee技术"></a>4.管道中的tee技术</h3><p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ghu70t1ulej30y00ayaba.jpg" alt="img"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#选项: -a追加</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '&#123;print $1&#125;' |awk '&#123;print $2&#125;'</span></span><br><span class="line">127.0.0.1</span><br><span class="line">10.0.0.100</span><br><span class="line"></span><br><span class="line">[root@lqz ~]<span class="comment"># cat ip.txt</span></span><br><span class="line">inet 127.0.0.1/8 scope host lo</span><br><span class="line">inet 10.0.0.100/24 brd 192.168.69.255 scope global ens32</span><br></pre></td></tr></table></figure>

<p>重定向与 tee 有他们在使用过程中有什么区别</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># date &gt; date.txt    #直接将内容写入date.txt文件中</span></span><br><span class="line">[root@lqz ~]<span class="comment"># date |tee date.txt #命令执行会输出至屏幕，但会同时保存一份至date.txt文件中</span></span><br></pre></td></tr></table></figure>

<p>5.xargs参数传递，主要让一些不支持管道的命令可以使用管道技术</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># which cat|xargs ls- l</span></span><br><span class="line"><span class="comment"># ls |xargs rm -fv</span></span><br><span class="line"><span class="comment"># ls |xargs cp -rvt /tmp/ -或-&gt; ls | xargs -I &#123;&#125; cp -rv &#123;&#125; /tmp/</span></span><br><span class="line"><span class="comment"># ls |xargs mv -t /tmp/   -或-&gt; ls | xargs -I &#123;&#125;  mv &#123;&#125; /tmp</span></span><br></pre></td></tr></table></figure></section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/linux/%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/09-LinuxACL%E6%8E%A7%E5%88%B6/">
        <span class="nav-arrow">← </span>
        
          linux/入门到精通/09-LinuxACL控制
        
      </a>
    
    
      <a class="nav-right" href="/linux/%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/11-Linux%E6%96%87%E4%BB%B6%E6%9F%A5%E6%89%BE/">
        
          linux/入门到精通/11-Linux文件查找
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#1-重定向概述"><span class="toc-nav-text">1.重定向概述</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-什么是重定向"><span class="toc-nav-text">1.什么是重定向</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-为何要使用重定向"><span class="toc-nav-text">2.为何要使用重定向</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-学习重定向的预备知识，标准输入与输出"><span class="toc-nav-text">3.学习重定向的预备知识，标准输入与输出</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-输出重定向"><span class="toc-nav-text">2.输出重定向</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-输入重定向"><span class="toc-nav-text">3.输入重定向</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-进程管道技术"><span class="toc-nav-text">4.进程管道技术</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-什么是管道"><span class="toc-nav-text">1.什么是管道</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-管道流程示意图"><span class="toc-nav-text">2.管道流程示意图</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-管道使用案例"><span class="toc-nav-text">3.管道使用案例</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-管道中的tee技术"><span class="toc-nav-text">4.管道中的tee技术</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/linux/入门到精通/10-Linux输入输出/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "linux/入门到精通/10-Linux输入输出",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>